# -*- encoding: utf-8 -*-

'''The setup code is split into two sections, one in setup.py which
contains very simple Python code and checks the Python version, and
this file, which contains code only parsable by 2.4+.

This code is taken from the FlickrAPI project.
'''

__author__ = u'Sybren Stüvel'

from distutils.core import setup, Distribution
import os
import sys

try:
    import docutils.core
except ImportError:
    docutils = None

from rtmapi import __version__

# This will be set to True when either the documentation is already
# there, or if we can build it.
documentation_available = False

class OurDistribution(Distribution):
    '''Distribution that also generates the rtmapi.html'''

    def run_command(self, command):
        '''Builds the documentation if needed, then passes control to
        the superclass' run_command(...) method.
        '''

        if command == 'install_data' and docutils:
            print 'creating doc/rtmapi.html'
            docutils.core.publish_file(writer_name='html',
                    source=open('doc/rtmapi.rst'),
                    source_path='doc',
                    destination=open('doc/rtmapi.html', 'w'),
                    destination_path='doc',
                    settings_overrides={'stylesheet_path':
                        'doc/documentation.css'}
            )
        Distribution.run_command(self, command)

data = {
    'name': 'rtmapi', 
    'version': __version__, 
    'url': 'http://code.google.com/p/rtmapi/', 
    'author': u'Mariano Draghi, based on work from Beej Jorgensen and Sybren Stuvel', 
    'author_email': 'mariano@chaghi.com.ar', 
    'description': 'Python interface to the Remember The Milk API', 
    'long_description': 'Python interface to the Remember The Milk API, '
        'heavily based on the rock-solid Python interface to the Flickr API '
        'by Beej Jorgensen and Sybren Stuvel. '
        'Includes support for authorized and non-authorized '
        'access, and all Remember The Milk API functions.', 
    'packages': ['rtmapi'],
    'data_files': [],
    'license': 'Python',
    'classifiers': [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Office/Business :: Scheduling',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    'distclass': OurDistribution
}

(major, minor) = sys.version_info[:2]
if major == 2 and minor < 5:
    # We still want to use this function, but Python 2.4 doesn't have
    # it built-in.
    def all(iterator):
        for item in iterator:
            if not item: return False
        return True

alldocs = ['doc/rtmapi.html', 'doc/documentation.css', 'doc/html4css1.css']

if docutils or all(os.path.exists(doc) for doc in alldocs):
    # Only include documentation if it can be built, or if it has been
    # built already
    data['data_files'].append(('share/doc/rtmapi-%s' % __version__, alldocs))
    documentation_available = True
else:
    print "======================================================================="
    print "WARNING: Unable to import docutils, documentation will not be included"
    print "Documentation for the latest version can be found at"
    print "http://code.google.com/p/rtmapi/"
    print "======================================================================="
    print

setup(**data)
